﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VideoSDK;

namespace TestVideoSDKHC
{
    public partial class Form1 : Form
    {
        //VideoSDKHC video = new VideoSDKHC();

        public Form1()
        {
            InitializeComponent();

            //this.video.RealStreamCallback = new IVideo.RealStreamCallback(RealStreamCall);
        }

        bool RealStreamCall(IntPtr buffer, uint bufferSize)
        {
            return true;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //if (video.LoginEncoder("192.168.1.188", 8000, "admin", "12345"))
            //    MessageBox.Show("User has Logined!");
            //else
            //    MessageBox.Show("User Login Faile!");

        }

        protected override void OnClosed(EventArgs e)
        {
            //if (this.video.IsLogined)
            //    this.video.LogoutEncoder();

            //if (this.video.IsOpened)
            //    this.video.CloseVideo();

            for (int i = 0; i < this.isInitialized.Length; i++)
            {
                if (this.isInitialized[i])
                {
                    if (this.userID[0] != -1)
                    {
                        HCNetSDK.NET_DVR_Logout_V30(this.userID[0]);

                        this.userID[0] = -1;
                    }

                    this.isInitialized[i] = !HCNetSDK.NET_DVR_Cleanup();
                }
            }
           

            base.OnClosed(e);
        }
        bool[] isInitialized = new bool[] { false, false };
        int[] userID = new int[] { -1, -1 };
        int[] playHandle = new[] { -1, -1 };
        NET_DVR_DEVICEINFO_V30[] deviceInfo = new NET_DVR_DEVICEINFO_V30[2];
        NET_DVR_CLIENTINFO[] clientInfo = new NET_DVR_CLIENTINFO[2];
        HCNetSDK.RealDataCallBack_V30[] realDataCallBack = new HCNetSDK.RealDataCallBack_V30[2];
        private void bOpen1_Click(object sender, EventArgs e)
        {
            //if (this.video.IsLogined && !this.video.IsOpened)
            //    this.video.OpenVideo(0, this.pBVideo1.Handle);

            Open(0, 0, 1, pBVideo1.Handle);
        }

        private void bOpen2_Click(object sender, EventArgs e)
        {
            Open(0, 1, 2, pBVideo2.Handle);
        }

        void Open(int userIndex, int clientIndex, int channel, IntPtr handle)
        {
            if (!this.isInitialized[userIndex])
                this.isInitialized[userIndex] = HCNetSDK.NET_DVR_Init();

            if (this.isInitialized[userIndex] && this.userID[userIndex] == -1)
            {
                this.userID[userIndex] = HCNetSDK.NET_DVR_Login_V30(tBIP.Text, 8000, "admin", "12345", out deviceInfo[userIndex]);

                if (this.userID[userIndex] < 0)
                {
                    uint error = HCNetSDK.NET_DVR_GetLastError();
                    MessageBox.Show(error.ToString() + " - 设备登陆失败！");
                }
            }

            if (this.isInitialized[userIndex] && this.userID[userIndex] >= 0)
            {
                this.realDataCallBack[clientIndex] = new HCNetSDK.RealDataCallBack_V30(RealDataCallback);
                this.clientInfo[clientIndex].hPlayWnd = handle;
                this.clientInfo[clientIndex].lChannel = channel;
                this.clientInfo[clientIndex].lLinkMode = ((int)StreamType.Master) | ((int)LinkMode.TCP);

                this.playHandle[clientIndex] = HCNetSDK.NET_DVR_RealPlay_V30(this.userID[userIndex], ref this.clientInfo[clientIndex], this.realDataCallBack[clientIndex], IntPtr.Zero, true);

                if (this.playHandle[clientIndex] < 0)
                {
                    uint error = HCNetSDK.NET_DVR_GetLastError();

                    MessageBox.Show(error.ToString() + " - 视频开启失败！");
                }

            }
        }

        private void RealDataCallback(int playHandle, uint dataType, IntPtr buffer, uint bufferSize, IntPtr user)
        {
        }
    }
}
